# -*- coding: utf-8 -*-#
#-------------------------------------------------------------------------------
# 建立者:        潘炎珍  
# Name:         Dbmanage
# Description:
# Author:       98745
# Date:         2019/4/6
#-------------------------------------------------------------------------------

from pymysql import *

def dbconnect():
    db=connect("127.0.0.1","root","root","xuexi",charset='utf8')
    return db

db=dbconnect()

def createtable(db):
    cursor=db.cursor()
    sql='''
    create table book
    (书名 varchar(30),
    作者 varchar(30),
    价格 int,
    评分 int,
    评价人数 int
    );'''
    try:
        cursor.execute(sql)
        db.commit
        return True
    except Exception as e:
        print(e)
        db.rollback()
    return False


def insert(db,data):
    '''向数据库插入信息'''
    cursor=db.cursor()
    sql="insert into book values('{}','{}','{}',{},{})".format(data['title'],data['authod'],data['price'],float(data['grade']),int(data['votenum']))
    try:
        cursor.execute(sql)
        db.commit()
        return True
    except Exception as e:
        print(e)
        db.rollback()
    return False
